package com.twoGroup.boot.service.imp;


import com.twoGroup.boot.mapper.UserMapper;
import com.twoGroup.boot.pojo.User;
import com.twoGroup.boot.service.UserService;
import com.twoGroup.boot.utils.IdNumberUtils;
import com.twoGroup.boot.utils.TeleNumUtils;
import com.twoGroup.boot.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    public static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;
    private static UserUtils userUtils = new UserUtils();
    private static IdNumberUtils idNumberUtils = new IdNumberUtils();
    private static TeleNumUtils teleNumUtils = new TeleNumUtils();

    @Transactional
    @Override
    public int addUser(String password, String email, String nickname) {
        Integer emailE = userMapper.selectCountEmail(email);
        Integer nicknameE = userMapper.queryNickname(nickname);
        if(emailE==0&&nicknameE==0){
            logger.info("--------------------------");
            return userMapper.addUser(password,email,nickname);
        }
        logger.info("==========================");
        return 0;
    }

    @Override
    public List<User> loginUser(String email) {
        return userMapper.loginUser(email);
    }

    @Transactional
    @Override
    public int updateUser(User user) {
        if(user.getNickname() == null){
            logger.info("昵称不能为空");
            return 0;
        }else if(!(user.getNickname().length()<=6 && user.getNickname().length()>=1)) {
            logger.info("昵称长度不合法");
            return 0;
        }
        //如果真实姓名不为空的话 就要判断年龄的合法性 如果为空的话就不管了
        else if(user.getrName() != null){
            if(user.getrName().length()>4||user.getrName().length()<2){
                logger.info("真实姓名不合法");
                return 0;
            }
        }
        //如果年龄不为空的话 就要判断年龄的合法性 如果为空的话就不管了
        else if(String.valueOf(user.getAge()) != null){
            if(user.getAge()<0||user.getAge()>100){
                logger.info("年龄不合法");
                return 0;
            }
        }
        else if(!user.getSex().equals("男")&&!user.getSex().equals("女")){
            logger.info("性别有误");
        }
        //如果手机号不为空的话 就要判断年龄的合法性 如果为空的话就不管了
        else if(user.getTelephone()!=null){
            if(!teleNumUtils.TelephoneBool(user.getTelephone())){
                logger.info("手机号不合法");
                return 0;
            }
        }
        //如果身份证不为空的话 就要判断年龄的合法性 如果为空的话就不管了
        else if(user.getIdCard()!=null){
            if((!idNumberUtils.userCardIdBool(user.getIdCard(),15,18))||user.getIdCard().length()==0){
                logger.info("身份证号不合法");
                return 0;
            }
        }
//            User user = new User();
            user.setNickname(user.getNickname());
            user.setAge(user.getAge());
            user.setIdCard(user.getIdCard());
            user.setSex(user.getSex());
            user.setrName(user.getrName());
            user.setTelephone(user.getTelephone());
            user.setUserId(user.getUserId());

        return userMapper.updateUser(user);
    }

    @Transactional
    @Override
    public int deleteUser(int userId) {
        return userMapper.deleteUser(userId);
    }

    @Transactional
    @Override
    public int resetPassword(String email, String password) {
        return userMapper.resetPassword(email,password);
    }

    @Override
    public Integer selectCountEmail(String email) {
        return userMapper.selectCountEmail(email);
    }

    @Override
    public Integer selectCountId(Integer userId) {
        return userMapper.selectCountId(userId);
    }

    @Override
    public List<User> queryUserId(Integer userId) {
        return userMapper.queryUserId(userId);
    }

    @Override
    public Integer booleanAdmin(String email) {
        return userMapper.booleanAdmin(email);
    }

    @Override
    public List<User> queryEmail(String email) {
        return userMapper.queryEmail(email);
    }

    @Override
    public Integer queryNickname(String nickname) {
        return userMapper.queryNickname(nickname);
    }

    @Override
    public User queryUserToEmail(String email) {
        return userMapper.queryUserToEmail(email);
    }

    @Transactional
    @Override
    public int applyBusiness(int userId) {
        User user = userMapper.selectUserId(userId);
        String sex = user.getSex();
        boolean age = user.getAge()>=18 && user.getAge()<=100;
        boolean telephone = teleNumUtils.TelephoneBool(user.getTelephone());
        boolean IdCard = idNumberUtils.userCardIdBool(user.getIdCard(), 15, 18);
        boolean rName = userUtils.UserStringBool(user.getrName(), 2, 3);
        if(sex!=null&&age&&telephone&&IdCard&&rName){
            if(user.getTelephone()!=null && user.getAge()!=null && user.getIdCard() != null && user.getrName()!=null && user.getSex()!=null){
                return userMapper.applyBusiness(userId);
            }else{
                return 0;
            }
        }else{
            return 0;
        }
    }

    @Override
    public User queryUser(String email) {
        return userMapper.queryUser(email);
    }

    @Override
    public Integer updatePassword(Integer userId, String password, String newPassword) {

        boolean newpasswordFlag = userUtils.UserStringBool(newPassword, 6, 16);
        boolean passwordFlag = userUtils.UserStringBool(password, 6, 16);
        try {
            if(passwordFlag){
                if(newpasswordFlag){
                    if(password.equals(userMapper.queryPassword(userId))){
                        return userMapper.updatePassword(userId, newPassword);
                    }else{
                        logger.info("原密码有误");
                        return 0;
                    }
                }else{
                    logger.info("密码长度有误");
                    return 2;
                }
            }else{
                logger.info("原密码长度有误");
                return 2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 2;
    }



    @Override
    public int updateHeadImg(Integer userId, String headImg) {
        return userMapper.updateHeadImg(userId, headImg);
    }

    @Override
    public User selectUserId(Integer userId) {
        return userMapper.selectUserId(userId);
    }

    @Override
    public User selectAllUser(Integer userId) {
        return userMapper.selectAllUser(userId);
    }
}
